#include <bits/stdc++.h>
#define sc(x) scanf("%lld", &(x))
#define pr(x) printf("%lld", (x))
#define rep(i, l, r) for (int i = l; i <= r; ++i)
using namespace std;
typedef long long ll;
const int N = 1e5 + 7;
const int mod = 1e9 + 7;
bool L[N], R[N];
ll a[N], n;
int main() {
    sc(n);
    rep(i, 1, n) sc(a[i]);
    ll mx = LLONG_MIN;
    for (int i = 1; i <= n; ++i)
        if (mx < a[i]) {
            L[i] = 1;
            mx = a[i];
        }
    ll mn = LLONG_MAX;
    for (int i = n; i; --i)
        if (mn > a[i]) {
            R[i] = 1;
            mn = a[i];
        }
    ll cnt = 0;
    rep(i, 1, n) if (L[i] && R[i])++ cnt;

    pr(cnt);
    putchar('\n');

    rep(i, 1, n) if (L[i] && R[i]) {
        pr(a[i]);
        if (--cnt) putchar(' ');
    }

    putchar('\n');
    // 这道题目真是垃圾
    // 第三个点PE是因为没有在第二行无条件换行
    // 而题目没有说明这一点
    return 0;
}